/*******************************************************************************
 * Copyright (c) 2012 Clever Devices.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 * 	Clever Devices Interns - 
 * 	Alex Frankel, Andria Young, Kevin Thomas, Nicholas Culpepper, Taner Oztimurlenk, Thomas Volberg
 ******************************************************************************/
package com.framework.menu;

import java.util.List;

import com.framework.beans.Prediction;
import com.framework.cleverstop_tentative.R;
import com.framework.menu.MenuAdapter.MenuHolder;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * Overridden to set the content of a row in the ArrivalsGrid
 * 
 * @author AFrankel
 *
 */
public class ArrivalsGridAdapter extends ArrayAdapter<Prediction>{

	public ArrivalsGridAdapter(Context context, int layoutResourceId, List<Prediction> objects) {
		super(context, layoutResourceId, objects);
		this.context = context;
		data = objects;
		resourceId = layoutResourceId;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ArrivalsHolder holder = null;
		
		if(row==null){
			LayoutInflater inflater = ((Activity)context).getLayoutInflater();
			row = inflater.inflate(resourceId,parent,false);
			
			holder = new ArrivalsHolder();
			holder.routeName = (TextView)row.findViewById(R.id.name);
			holder.prediction = (TextView)row.findViewById(R.id.prediction);
			holder.busId = (TextView)row.findViewById(R.id.busId);
			holder.destination = (TextView)row.findViewById(R.id.destination);
			holder.background = (LinearLayout)row.findViewById(R.id.arrivalsGridRow);
			
			row.setTag(holder);
		}
		else{
			holder = (ArrivalsHolder)row.getTag();
		}
		
		Prediction gridItem = data.get(position);
		
		holder.routeName.setText(gridItem.getRoute());
		holder.prediction.setText(String.valueOf(gridItem.getPrediction()));
		holder.busId.setText(gridItem.getVehicleId());
		holder.destination.setText(gridItem.getDestination());
		
		if(gridItem.getVehicleId().equals(busId))
			holder.background.setBackgroundResource(R.drawable.list_item_different);
		
		
		
		return row;
	}

	
	Context context;
	List<Prediction> data;
	int resourceId;

	
	static class ArrivalsHolder{
		public ArrivalsHolder(){}
		
		TextView routeName;
		TextView prediction;
		TextView busId;
		TextView destination;
		LinearLayout background;
	}
	
	public final String busId = "1234";
}
